package com.comac.print.component.app.eventhandle;

import com.alibaba.cola.dto.Response;
import com.alibaba.cola.event.EventHandler;
import com.alibaba.cola.event.EventHandlerI;
import com.alibaba.fastjson.JSON;
import com.comac.print.component.app.command.CommandExe;
import com.comac.print.component.app.common.PrintCommon;
import com.comac.print.component.app.domainevent.PrintRecordEvent;
import com.comac.print.component.app.enums.ScenarioEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

/**
 * @Description 打印事件执行器
 * @Author ye
 * @Date 2022/4/19 10:30
 */
@Slf4j
@EventHandler
public class PrintHandler implements EventHandlerI<Response, PrintRecordEvent>{

    @Autowired
    private ApplicationContext applicationContext;


    @Override
    public Response execute(PrintRecordEvent printRecordEvent) {
        Response response = null;
        PrintCommon printCommon = printRecordEvent.getPrintCommon();
        // 打印执行器-执行
        ScenarioEnum scenarioEnum = null;//ScenarioEnum.getScenarioEnumByCode(printCommon.getCode());
        if(scenarioEnum == null){
            return Response.buildFailure("-1", "非法操作！");
        }
        Class<? extends CommandExe> clazz = scenarioEnum.getClazz();
        CommandExe bean = applicationContext.getBean(clazz);
        response = bean.doExecute(printCommon);
        log.info("print result is {}", JSON.toJSONString(response));
        return response;
    }
}
